var express = require('express');
var router = express.Router();

let fs = require('fs');
const path = require('path')
const mysql = require('mysql')
const connection = mysql.createConnection({
  host: '127.0.0.1',
  user: 'root',
  password: 'root',
  // 数据库名称
  database: 'school-c2c'
})

/* GET home page. */1
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

//获取普通用户
router.get('/getusers', (req, res) => {
  const sql = 'select * from users'

  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取管理员用户
router.get('/getadminusers', (req, res) => {
  const sql = 'select * from admin'

  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//编辑普通用户111111
router.put('/editusers/:id', (req, res) => {
  const sql = 'update users set ? where id = ?'
  console.log(req.body);
  const id = req.params.id
  const body = req.body
  connection.query(sql, [body, id], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: '编辑成功'
    })
  })
})

//删除普通用户
router.delete('/deleteuser/:id', (req, res) => {
  const sql = 'delete from users where id = ?'
  const id = req.params.id
  connection.query(sql, id, (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: '删除成功'
    })
  })
})

//普通用户登录111
router.post('/login', (req, res) => {
  console.log(req.body);
  let userObj = req.body
  const sql = 'select * from users where username=? and password=?'
  let params = [
    userObj.username,
    userObj.password
  ]
  console.log(params);
  connection.query(sql, params, (err, results) => {
    if (results && results.length >= 1) {
      res.send({
        code: 200,
        message: '登录成功',
        type: '普通用户',
        user: results
      })
    } else {
      res.send({
        code: 0,
        message: '登录失败'
      })
    }
  })
})

//管理员登录11
router.post('/adminlogin', (req, res) => {
  console.log(req.body);
  let userObj = req.body
  const sql = 'select * from admin where username=? and password=?'
  let params = [
    userObj.username,
    userObj.password
  ]
  console.log(params);
  connection.query(sql, params, (err, results) => {
    if (err) throw err;
    if (results && results.length >= 1) {
      res.send({
        code: 200,
        message: '登陆成功',
        type: '超级管理员'
      })
    } else {
      res.send({
        code: 0,
        message: '登陆失败'
      })
    }
  })
})

//注册账号1
router.post("/register", function (req, res, next) {
  //1.得到前端传过来的数据
  console.log(req.body);
  let userObj = req.body;
  //2. sql
  let checkparams = [
    userObj.username
  ]
  let checksql = 'select * from users where username=?'
  connection.query(checksql, checkparams, (err, result) => {
    if (err) throw err;
    if (result && result.length >= 1) {
      res.send({
        code: 0,
        message: '该账户已存在'
      })
    } else {
      let sql = " INSERT INTO `users` (`username`,`password`) VALUE (?,?);";
      let params = [
        userObj.username,
        userObj.password,
      ];
      connection.query(sql, params, (err, result) => {
        let isReg = result && result.affectedRows >= 1;
        let messages = {
          msg: isReg ? "注册成功" : "注册失败",
          status: isReg ? 200 : -200
        }
        res.send(messages);
      })
    }
  })
})

//修改密码1
router.post('/change', (req, res) => {
  console.log(req.body);
  const body = req.body
  const sql = 'UPDATE  users SET PASSWORD = ?  WHERE username= ? AND PASSWORD= ?'
  connection.query(sql, [body.newpassword, body.username, body.password], (err, result) => {
    let isReg = result && result.affectedRows >= 1;
    let messages = {
      msg: isReg ? "修改成功" : "修改失败",
      status: isReg ? 200 : -200
    }
    res.send(messages)
  })
})


//获取服饰1
router.get('/shoesandclothes', (req, res) => {
  const sql = 'select * from goods where col = "shoesandclothes"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取3c
router.get('/digital', (req, res) => {
  const sql = 'select * from goods where col = "digital"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取books
router.get('/books', (req, res) => {
  const sql = 'select * from goods where col = "books"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取life
router.get('/life', (req, res) => {
  const sql = 'select * from goods where col = "life"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取美妆
router.get('/beauty', (req, res) => {
  const sql = 'select * from goods where col = "beauty"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取其他
router.get('/others', (req, res) => {
  const sql = 'select * from goods where col = "others"'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取全部11
router.get('/all', (req, res) => {
  const sql = 'select * from goods'
  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})


//搜索商品1111111
router.get('/search/:value', (req, res) => {
  const sql = 'select * from goods where name like  ? '
  console.log(req.params);
  const value = "%" + req.params.value + "%"
  connection.query(sql, [value], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})



//图片上传1111
const multer = require('multer')
const upload = multer({ dest: path.join(__dirname, '../uploads') })

router.post('/upload', upload.single('file'), (req, res) => {
  const file = req.file
  file.url = 'http://127.0.0.1:3000/uploads/' + file.filename
  res.send(file)
})


//进入详情页
router.post('/goodsdetail/:id', (req, res) => {
  const id = req.params.id
  const col = req.body.col
  const sql = 'select * from goods where id = ? and col = ?'
  connection.query(sql, [id, col], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取评论
router.post('/getcomments/:id', (req, res) => {
  const id = req.params.id
  const col = req.body.col
  const sql = 'select comments from goods where id = ? and col = ?'
  connection.query(sql, [id, col], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//发表评论
router.put('/comments/:col/:id', (req, res) => {
  const body = JSON.stringify(req.body)
  console.log(body);
  const id = req.params.id
  const col = req.params.col
  console.log(req.params.id);
  const sql = 'update goods set comments = ? where id = ? and col = ?'
  connection.query(sql, [body, id, col], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//收藏商品1
router.post('/shoucang/:user', (req, res) => {
  const user = req.params.user
  const body = JSON.stringify(req.body)
  const sql = 'UPDATE users SET shoucang = ? WHERE username = ?;'
  connection.query(sql, [body, user], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取我的收藏
router.post('/getshoucang/:user', (req, res) => {
  const user = req.params.user
  const sql = 'select shoucang from users WHERE username = ?;'
  connection.query(sql, [user], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//购买物品(买家信息更新)1111111111
router.post('/shopping/:user', (req, res) => {
  const user = req.params.user
  console.log(req.body);
  const body = JSON.stringify(req.body)
  const sql = 'UPDATE users SET dingdan = ? WHERE username = ?;'
  connection.query(sql, [body, user], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//购买物品(卖家信息更新)1111111
router.post('/sellshopping/:id', (req, res) => {
  const id = req.params.id
  const body = JSON.stringify(req.body)
  const sql = 'UPDATE goods SET buyer = ? WHERE id = ?;'
  connection.query(sql, [body, id], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//更新商品状态
router.post('/goodsst/:id', (req, res) => {
  const id = req.params.id
  const st = req.body.st
  const sql = 'UPDATE goods SET st = ? WHERE id = ?;'
  connection.query(sql, [st, id], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取我的订单1111
router.post('/getshopping/:user', (req, res) => {
  const user = req.params.user
  const sql = 'select dingdan from users WHERE username = ?;'
  connection.query(sql, [user], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//获取我的出售111111
router.post('/getmysell/:user', (req, res) => {
  const user = req.params.user
  const sql = 'select * from goods WHERE seller = ?;'
  connection.query(sql, [user], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})

//上传商品11111111111
router.post('/upbaby', (req, res) => {
  console.log(req.body);
  const body = req.body
  const sql = 'INSERT INTO goods (name,price,src,place,data,pc,col,seller,phone) VALUES ( ? ,? , ? , ? , ? , ? , ? , ? , ?);'
  connection.query(sql, [body.name, body.price, body.icon, body.place, body.data, body.pc, body.col, body.seller, body.phone], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})



//11111
router.get('/getadmin', (req, res) => {
  const sql = 'select * from admin'

  connection.query(sql, [], (err, results) => {
    if (err) throw err;
    res.send({
      err_code: 0,
      message: results
    })
  })
})
module.exports = router;
